import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:moose_app/ui/screens/discovery/ranking/ranking_content.dart';
import 'package:moose_app/ui/screens/discovery/square/square_content.dart';
import 'package:moose_app/ui/values/values.dart';
import 'package:moose_app/ui/widgets/common/common_more_sheet.dart';
import 'package:moose_app/ui/widgets/icon_button.dart';
import 'package:moose_app/ui/widgets/scroll/scroller_configuration_wrapper.dart';
import 'package:moose_app/ui/widgets/sticky_tabbar_delegate.dart';

import 'circle/circle_content.dart';
import 'follow/follow_content.dart';
import 'recommend/recommend_content.dart';

class ATHDiscoveryScreen extends StatefulWidget {
  @override
  _ATHDiscoveryScreenState createState() => _ATHDiscoveryScreenState();
}

class _ATHDiscoveryScreenState extends State<ATHDiscoveryScreen>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  TabController _tabController;

  List<String> _tabsText = ["关注", "推荐", "排行榜", "广场", "圈子"];

  List<Tab> _tabs = [];

  @override
  void initState() {
    super.initState();
    _tabsText.forEach((element) {
      _tabs.add(Tab(
        text: element,
      ));
    });

    _tabController =
        TabController(initialIndex: 1, length: _tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ATHScrollerConfigurationWrapper(
      child: CustomScrollView(
        slivers: <Widget>[
          _buildAppBar(),
          SliverPersistentHeader(
            pinned: true,
            delegate: ATHStickyTabBarDelegate(
                child: _buildTabBar(),
                onMorePress: () {
                  print("More Icon Press");
                  _showMoreSheet();
                }),
          ),
          SliverFillRemaining(
            child: _buildTabBarView(),
          ),
        ],
      ),
    );
  }

  _showMoreSheet() async {
    await showCupertinoModalBottomSheet(
      expand: false,
      context: context,
      backgroundColor: Colors.white,
      builder: (BuildContext context) {
        return ATHCommonMoreSheet();
      },
    );
  }

  TabBar _buildTabBar() {
    return TabBar(
      isScrollable: true,
      controller: _tabController,
      tabs: _tabs,
      indicatorColor: kColor33,
      indicatorSize: TabBarIndicatorSize.label,
      labelStyle: TextStyle(fontSize: kFontSize32, fontWeight: FontWeight.bold),
      labelColor: kColor33,
      unselectedLabelStyle: TextStyle(fontSize: kFontSize30),
      unselectedLabelColor: kColor99,
    );
  }

  TabBarView _buildTabBarView() {
    List<Widget> tabContents = [];
    // "关注", "推荐", "排行榜", "广场", "圈子"
    for (int index = 0; index < _tabsText.length; index++) {
      if (index == 0) {
        tabContents.add(ATHFollowContent());
      } else if (index == 1) {
        tabContents.add(ATHRecommendContent());
      } else if (index == 2) {
        tabContents.add(ATHRankingContent());
      } else if (index == 3) {
        tabContents.add(ATHSquareContent());
      } else if (index == 4) {
        tabContents.add(ATHCircleContent());
      } else {
        tabContents.add(Center(
          child: Text(_tabsText[index]),
        ));
      }
    }
    return TabBarView(controller: _tabController, children: tabContents);
  }

  Widget _buildAppBar() {
    return SliverAppBar(
      pinned: true,
      elevation: 0,
      centerTitle: false,
      title: Text("发现"),
      leading: ATHIconButton(
        iconPath: "assets/icons/icon_location.svg",
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
